import calendar
import json
import os

from flask import Flask, Response, request, send_file

import dao

app = Flask(__name__)


@app.route('/')
def hello_world():  # put application's code here
    return 'Hello World!'


# 新增用户
@app.route('/add', methods=['GET', 'POST'])
def add():
    global result, jsonResult
    if request.method == 'POST':
        try:
            data = request.get_json()
            dao.Add(data)
            result = {'status': 200}
            jsonResult = json.dumps(result)
            return Response(jsonResult, mimetype='application/json')
        except:
            result['status'] = 400
            return Response(jsonResult, mimetype='application/json')


# 登录
@app.route('/login', methods=['GET', 'POST'])
def login():
    global result, jsonResult
    if request.method == 'POST':
        try:
            data = request.get_json()
            user_id = dao.login(data)
            if user_id:
                result = {'status': 200, 'id': user_id, 'ret': 1}
            else:
                result = {'status': 200, 'ret': 0}
            jsonResult = json.dumps(result)
            return Response(jsonResult, mimetype='application/json')
        except:
            result['status'] = 400
            return Response(jsonResult, mimetype='application/json')


# 开设课程
@app.route('/addCourse', methods=['GET', 'POST'])
def addCourse():
    global result, jsonResult
    if request.method == 'POST':
        try:
            data = request.get_json()
            flag = dao.AddCourse(data)
            if flag:
                result = {'status': 200}
                jsonResult = json.dumps(result)
                return Response(jsonResult, mimetype='application/json')
            else:
                result['status'] = 400
                return Response(jsonResult, mimetype='application/json')
        except:
            result['status'] = 400
            return Response(jsonResult, mimetype='application/json')


# 加入课程
@app.route('/joinCourse', methods=['GET', 'POST'])
def joinCourse():
    global result, jsonResult
    if request.method == 'POST':
        try:
            data = request.get_json()
            flag = dao.JoinCourse(data)
            if flag:
                result = {'status': 200}
                jsonResult = json.dumps(result)
                return Response(jsonResult, mimetype='application/json')
            else:
                result['status'] = 400
                return Response(jsonResult, mimetype='application/json')
        except:
            result['status'] = 400
            return Response(jsonResult, mimetype='application/json')


# 布置作业
@app.route('/addHomework', methods=['GET', 'POST'])
def addHomework():
    global result, jsonResult
    if request.method == 'POST':
        try:
            data = request.get_json()
            flag = dao.AddHomework(data)
            if flag:
                result = {'status': 200}
                jsonResult = json.dumps(result)
                return Response(jsonResult, mimetype='application/json')
            else:
                result['status'] = 400
                return Response(jsonResult, mimetype='application/json')
        except:
            result['status'] = 400
            return Response(jsonResult, mimetype='application/json')


# 按教师id查找课程
@app.route('/selectCourseBT', methods=['GET', 'POST'])
def selectCourseBT():
    global result, jsonResult
    if request.method == 'POST':
        try:
            data = request.get_json()
            list_courses = dao.selectAllCourseByTeacher(data['teacher_id'])
            if list_courses:
                result = {'status': 200, 'courses': list_courses, 'courses_num': len(list_courses)}
                jsonResult = json.dumps(result)
                return Response(jsonResult, mimetype='application/json')
            else:
                result['status'] = 400
                return Response(jsonResult, mimetype='application/json')
        except:
            result['status'] = 400
            return Response(jsonResult, mimetype='application/json')


# 按学生id查找课程
@app.route('/selectCourseBS', methods=['GET', 'POST'])
def selectCourseBS():
    global result, jsonResult
    if request.method == 'POST':
        try:
            data = request.get_json()
            list_courses = dao.selectAllCourseByStudent(data['student_id'])
            if list_courses:
                result = {'status': 200, 'courses': list_courses, 'courses_num': len(list_courses)}
                jsonResult = json.dumps(result)
                return Response(jsonResult, mimetype='application/json')
            else:
                result['status'] = 400
                return Response(jsonResult, mimetype='application/json')
        except:
            result['status'] = 400
            return Response(jsonResult, mimetype='application/json')


# 按课程id查找作业
@app.route('/selectHomeworkBC', methods=['GET', 'POST'])
def selectHomeworkBC():
    global result, jsonResult
    if request.method == 'POST':
        try:
            data = request.get_json()
            list_homeworks = dao.selectAllHomeworkByCourse(data['course_id'])
            result = {'status': 200, 'homeworks': list_homeworks, 'homeworks_num': len(list_homeworks)}
            jsonResult = json.dumps(result)
            return Response(jsonResult, mimetype='application/json')
        except:
            result['status'] = 400
            return Response(jsonResult, mimetype='application/json')


# 交作业
@app.route('/submitHomework', methods=['POST'])
def submit_homework():
    global result, jsonResult
    file = request.files.get('file')
    if file is None:
        # 表示没有发送文件
        return {
            'message': "文件上传失败"
        }
    file_name = file.filename.replace(" ", "")
    file.save(os.path.dirname(__file__) + '/homework/' + file_name)  # 保存文件

    data = request.form.to_dict()
    data['name'] = file_name
    try:
        ret = dao.submitHomework(data)
        result['status'] = 200
        jsonResult = json.dumps(result)
        return Response(jsonResult, mimetype='application/json')
    except:
        result['status'] = 400
        return Response(jsonResult, mimetype='application/json')


# 按作业id查找学生作业
@app.route('/selectStudentHW', methods=['GET', 'POST'])
def selectStudentHW():
    global result, jsonResult
    if request.method == 'POST':
        try:
            data = request.get_json()
            list_studentHW = dao.getStudentHW(data)
            if list_studentHW:
                result = {'status': 200, 'studentHWs': list_studentHW, 'studentHWs_num': len(list_studentHW)}
                print(result)
                jsonResult = json.dumps(result)
                return Response(jsonResult, mimetype='application/json')
            else:
                result['status'] = 400
                return Response(jsonResult, mimetype='application/json')
        except:
            result['status'] = 400
            return Response(jsonResult, mimetype='application/json')


# 下载作业
@app.route('/file/<homework_name>', methods=['GET'])
def getfile(homework_name):
    print(homework_name)
    return send_file(os.path.dirname(__file__) + '/homework/' + homework_name)
    # return 'hello'


# 交分数
@app.route('/submitScore', methods=['GET', 'POST'])
def submitScore():
    global result, jsonResult
    if request.method == 'POST':
        try:
            data = request.get_json()
            flag = dao.submitScore(data)
            if flag:
                result = {'status': 200}
                jsonResult = json.dumps(result)
                return Response(jsonResult, mimetype='application/json')
            else:
                result['status'] = 400
                return Response(jsonResult, mimetype='application/json')
        except:
            result['status'] = 400
            return Response(jsonResult, mimetype='application/json')


# 获取学生
@app.route('/getStudent', methods=['GET'])
def getStudent():
    students = dao.getStudent()
    Result = json.dumps(students)
    return Response(Result, mimetype='application/json')


# 获取老师
@app.route('/getTeacher', methods=['GET'])
def getTeacher():
    teachers = dao.getTeacher()
    Result = json.dumps(teachers)
    return Response(Result, mimetype='application/json')


# 审核老师通过
@app.route('/allowTeacher', methods=['GET', 'POST'])
def allowTeacher():
    global result, jsonResult
    if request.method == 'POST':
        try:
            datas = request.get_json()
            flag = dao.allowTeacher(datas)
            if flag:
                result = {'status': 200}
                jsonResult = json.dumps(result)
                return Response(jsonResult, mimetype='application/json')
            else:
                result['status'] = 400
                return Response(jsonResult, mimetype='application/json')
        except:
            result['status'] = 400
            return Response(jsonResult, mimetype='application/json')


if __name__ == '__main__':
    app.run()
